package com.ticket;

import details.Concert;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class UserLogin {
    public static int ID;
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet = null;
    private static Scanner scanner = new Scanner(System.in);

    public static void VerifyUser() {
        boolean flag = false;
        int count = 3;
        int power = 0;
        String username = null;
        String password = null;

        while (true) {
            System.out.print("请输入登录名:");
            username = scanner.next();
            System.out.print("请输入密码:");
            password = scanner.next();

            try {
                connection = DBUtils.getConnection();

                String sql = "select * from user where username = ? and password = ?";

                preparedStatement = connection.prepareStatement(sql);

                preparedStatement.setString(1, username);
                preparedStatement.setString(2, password);
                resultSet = preparedStatement.executeQuery();

                while (resultSet.next()) {
                    flag = true;
                    ID = resultSet.getInt("id");
                    power = resultSet.getInt("power");
                    break;
                }

                if (flag) {
                    if (power == 0) {
                        index();
                        break;
                    } else {
                        managerindex();
                        break;
                    }
                } else {
                    if (count == 0) {
                        System.out.println("登录失败,请稍后重试!");
                        break;
                    }
                    System.out.println("输入信息有误，你还有" + count + "次尝试机会!!!");
                    count--;

                }
                DBUtils.close(resultSet, preparedStatement, connection);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void index() { //用户登录进来的页面
        while (true) {
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            System.out.println("1.演唱会详情\t2.客服\t0.返回\t-1.退出");
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            System.out.print("当前[用户]请输入功能号:");
            String i = scanner.next();
            if (i.equals("1")) {
                Concert.concert();//查看演唱会详情
                break;
            } else if (i.equals("2")) {
                ChatService.Question();
            } else if (i.equals("0")) {
                HomePage.home();
                break;
            } else if (i.equals("-1")) {
                break;
            } else {
                System.out.println("输入有误，请重新输入");
            }
        }
    }


    public static void managerindex() { //管理员登录进来的页面
        while (true) {
            System.out.println("1.查看演唱会信息\t2.添加演唱会信息\t3.修改演唱会信息\t4.删除演唱会信息\t0.返回\t-1.退出");
            System.out.print("当前[管理员]请输入功能号:");
            String i = scanner.next();
            if (i.equals("1")) {
                ManagerLogin.showall();
            } else if (i.equals("2")) {
                ManagerLogin.addticket();
            } else if (i.equals("3")) {
                ManagerLogin.modifyticket();
            } else if (i.equals("4")) {
                ManagerLogin.deleteticket();
            } else if (i.equals("0")) {
                HomePage.home();
                break;
            } else if (i.equals("-1")) {
                break;
            } else {
                System.out.println("输入有误，请重新输入");
            }
        }
    }


}
